package GUILayer;

import ControlLayer.PersonCtr;

import ModelLayer.Employee;
import ModelLayer.EmployeeRecords;
import ModelLayer.RealException;

import java.awt.BorderLayout;

import javax.swing.BorderFactory;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.BoxLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

import java.awt.GridLayout;
import java.awt.FlowLayout;
import java.awt.CardLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JPasswordField;
import javax.swing.JComboBox;

public class EmployeeUI extends JPanel {

	private static final long serialVersionUID = 1L;

	private PersonCtr personCtr;
	private EmployeeRecords employeeRecords;

	private JTextField showAddressField;
	private JTextField showIdField;
	private JTextField showNameField;

	private JTextField editAddressField;
	private JTextField editIdField;
	private JTextField editNameField;
	private JTextField editPhoneField;

	private JPanel editEmployee;

	private DefaultTableModel tableModel;
	private JTable table;

	private JPanel showEmployee;
	private JPanel showAllEmployees;
	private JTextField showAllIdField;

	private JTextField createAddressField;
	private JTextField createNameField;
	private JTextField createPhoneField;

	private JPanel createEmployee;

	private JPanel employeesContainer;
	private JPasswordField showPasswordField;
	private JTextField showPhoneField;
	private JPasswordField editPasswordField;
	private JPasswordField createPasswordField;
	private JTextField showPositionField;
	private JTextField editPositionField;
	private JTextField createPositionField;

	private JComboBox<String> editPositionCombo;

	private JComboBox<String> createPositionCombo;

	public EmployeeUI() {
		personCtr = new PersonCtr();
		employeeRecords = EmployeeRecords.getInstance();
		initialize();
	}

	public void initialize() {

		tableModel = new DefaultTableModel() {
			private static final long serialVersionUID = 1L;

			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
		tableModel.addColumn("ID");
		tableModel.addColumn("Name");
		tableModel.addColumn("Address");

		this.setLayout(new BorderLayout(0, 0));

		JPanel showAllMenu = new JPanel();
		this.add(showAllMenu, BorderLayout.NORTH);
		showAllMenu.setLayout(new BorderLayout(0, 0));
		showAllMenu
				.setBorder(BorderFactory.createTitledBorder("Employee Menu"));

		JPanel showAllMenuLeft = new JPanel();
		showAllMenu.add(showAllMenuLeft, BorderLayout.WEST);

		JButton btnCreateNewEmployee = new JButton("Create new employee");
		btnCreateNewEmployee.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				createPositionCombo.setSelectedIndex(1);
				createPositionCombo.setSelectedIndex(0);
				CardLayout cl = (CardLayout) employeesContainer.getLayout();
				cl.show(employeesContainer, "createEmployee");
			}
		});
		showAllMenuLeft.add(btnCreateNewEmployee);

		JPanel showAllMenuRight = new JPanel();
		showAllMenu.add(showAllMenuRight, BorderLayout.EAST);

		JLabel showAllIdLbl = new JLabel("Id:");
		showAllMenuRight.add(showAllIdLbl);

		showAllIdField = new JTextField();
		showAllMenuRight.add(showAllIdField);
		showAllIdField.setColumns(5);

		JButton showAllBtnSearch = new JButton("Search");
		showAllBtnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showEmployee(Integer.parseInt(showAllIdField.getText()));
				showAllIdField.setText("");
			}
		});
		showAllMenuRight.add(showAllBtnSearch);

		employeesContainer = new JPanel();
		this.add(employeesContainer);
		employeesContainer.setLayout(new CardLayout(0, 0));

		showEmployee = new JPanel();
		employeesContainer.add(showEmployee, "showEmployee");
		showEmployee.setLayout(new BorderLayout(0, 0));

		JPanel showEmployeeInfoContainer = new JPanel();
		showEmployee.add(showEmployeeInfoContainer, BorderLayout.NORTH);
		showEmployeeInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		showEmployeeInfoContainer.setBorder(BorderFactory
				.createTitledBorder("Employee Info"));

		JPanel showEmployeeInfo = new JPanel();
		showEmployeeInfo.setLayout(new BoxLayout(showEmployeeInfo,
				BoxLayout.X_AXIS));
		showEmployeeInfoContainer.add(showEmployeeInfo);

		JPanel showEmployeeLeft = new JPanel();
		showEmployeeInfo.add(showEmployeeLeft);
		showEmployeeLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel showIdLabelPanel = new JPanel();
		showIdLabelPanel.setLayout(new BoxLayout(showIdLabelPanel,
				BoxLayout.X_AXIS));
		showEmployeeLeft.add(showIdLabelPanel);

		JLabel showIdLabel = new JLabel("ID");
		showIdLabelPanel.add(showIdLabel);
		showIdLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showNameLabelPanel = new JPanel();
		showNameLabelPanel.setLayout(new BoxLayout(showNameLabelPanel,
				BoxLayout.X_AXIS));
		showEmployeeLeft.add(showNameLabelPanel);

		JLabel showNameLabel = new JLabel("Name");
		showNameLabelPanel.add(showNameLabel);
		showNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showAddressLabelPanel = new JPanel();
		showAddressLabelPanel.setLayout(new BoxLayout(showAddressLabelPanel,
				BoxLayout.X_AXIS));
		showEmployeeLeft.add(showAddressLabelPanel);

		JLabel showAddressLabel = new JLabel("Address");
		showAddressLabelPanel.add(showAddressLabel);
		showAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showPhoneLabelPanel = new JPanel();
		showPhoneLabelPanel.setLayout(new BoxLayout(showPhoneLabelPanel,
				BoxLayout.X_AXIS));
		showEmployeeLeft.add(showPhoneLabelPanel);

		JLabel showPhoneLabel = new JLabel("Phone");
		showPhoneLabelPanel.add(showPhoneLabel);
		showPhoneLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel showPasswordLabelPanel = new JPanel();
		showEmployeeLeft.add(showPasswordLabelPanel);
		showPasswordLabelPanel.setLayout(new BoxLayout(showPasswordLabelPanel,
				BoxLayout.X_AXIS));

		JLabel showPasswordLabel = new JLabel("Password");
		showPasswordLabel.setHorizontalAlignment(SwingConstants.CENTER);
		showPasswordLabelPanel.add(showPasswordLabel);

		JPanel showPositionLabelPanel = new JPanel();
		showEmployeeLeft.add(showPositionLabelPanel);
		showPositionLabelPanel.setLayout(new BoxLayout(showPositionLabelPanel,
				BoxLayout.X_AXIS));

		JLabel showPositionLabel = new JLabel("Position");
		showPositionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		showPositionLabelPanel.add(showPositionLabel);

		JPanel showEmployeeRight = new JPanel();
		showEmployeeRight.setLayout(new BoxLayout(showEmployeeRight,
				BoxLayout.Y_AXIS));
		showEmployeeInfo.add(showEmployeeRight);

		JPanel showIdFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showEmployeeRight.add(showIdFieldPanel);

		showIdField = new JTextField();
		showIdFieldPanel.add(showIdField);
		showIdField.setHorizontalAlignment(SwingConstants.CENTER);
		showIdField.setEditable(false);
		showIdField.setColumns(5);

		JPanel showNameFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showEmployeeRight.add(showNameFieldPanel);

		showNameField = new JTextField();
		showNameFieldPanel.add(showNameField);
		showNameField.setHorizontalAlignment(SwingConstants.CENTER);
		showNameField.setEditable(false);
		showNameField.setColumns(20);

		JPanel showAddressFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		showEmployeeRight.add(showAddressFieldPanel);

		showAddressField = new JTextField();
		showAddressFieldPanel.add(showAddressField);
		showAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		showAddressField.setEditable(false);
		showAddressField.setColumns(25);

		JPanel showPhoneFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		showEmployeeRight.add(showPhoneFieldPanel);

		showPhoneField = new JTextField();
		showPhoneField.setHorizontalAlignment(SwingConstants.CENTER);
		showPhoneField.setEditable(false);
		showPhoneField.setColumns(15);
		showPhoneFieldPanel.add(showPhoneField);

		JPanel showPasswordFieldPanel = new JPanel();
		showEmployeeRight.add(showPasswordFieldPanel);
		showPasswordFieldPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));

		showPasswordField = new JPasswordField();
		showPasswordField.setHorizontalAlignment(SwingConstants.CENTER);
		showPasswordFieldPanel.add(showPasswordField);
		showPasswordField.setEditable(false);
		showPasswordField.setColumns(15);

		JPanel showPositionFieldPanel = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) showPositionFieldPanel
				.getLayout();
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		showEmployeeRight.add(showPositionFieldPanel);

		showPositionField = new JTextField();
		showPositionField.setHorizontalAlignment(SwingConstants.CENTER);
		showPositionField.setEditable(false);
		showPositionField.setColumns(15);
		showPositionFieldPanel.add(showPositionField);

		JPanel showBottomMenu = new JPanel();
		showEmployee.add(showBottomMenu, BorderLayout.CENTER);
		showBottomMenu.setLayout(new BorderLayout(0, 0));

		JPanel showBottomMenuRight = new JPanel();
		showBottomMenu.add(showBottomMenuRight, BorderLayout.CENTER);

		JButton showBtnEdit = new JButton("Edit");
		showBtnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				editEmployee((int) Integer.parseInt(showIdField.getText()));
			}
		});
		showBottomMenuRight.add(showBtnEdit);

		JButton showBtnDelete = new JButton("Delete");
		showBtnDelete.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int reply = JOptionPane.showConfirmDialog(null,
						"Are you sure you want to delete this employee ?",
						"Delete Employee", JOptionPane.YES_NO_OPTION,
						JOptionPane.WARNING_MESSAGE);
				if (reply == JOptionPane.YES_OPTION) {
					boolean success = true;
					try {
						personCtr.deleteEmployee(Integer.parseInt(showIdField
								.getText()));
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The Employee was deleted", "Delete Employee",
								JOptionPane.INFORMATION_MESSAGE);
						showAllEmployees();
					}
				}
			}
		});
		showBottomMenuRight.add(showBtnDelete);

		JPanel showBottomMenuLeft = new JPanel();
		showBottomMenu.add(showBottomMenuLeft, BorderLayout.WEST);

		JButton showBtnBack = new JButton("Back");
		showBtnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllEmployees();
			}
		});
		showBottomMenuLeft.add(showBtnBack);

		editEmployee = new JPanel();
		employeesContainer.add(editEmployee, "editEmployee");
		editEmployee.setLayout(new BorderLayout(0, 0));

		JPanel editEmployeeInfoContainer = new JPanel();
		editEmployee.add(editEmployeeInfoContainer, BorderLayout.NORTH);
		editEmployeeInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		editEmployeeInfoContainer.setBorder(BorderFactory
				.createTitledBorder("Edit Employee"));

		JPanel editEmployeeInfo = new JPanel();
		editEmployeeInfo.setLayout(new BoxLayout(editEmployeeInfo,
				BoxLayout.X_AXIS));
		editEmployeeInfoContainer.add(editEmployeeInfo);

		JPanel editEmployeeLeft = new JPanel();
		editEmployeeInfo.add(editEmployeeLeft);
		editEmployeeLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel editIdLabelPanel = new JPanel();
		editIdLabelPanel.setLayout(new BoxLayout(editIdLabelPanel,
				BoxLayout.X_AXIS));
		editEmployeeLeft.add(editIdLabelPanel);

		JLabel editIdLabel = new JLabel("ID");
		editIdLabelPanel.add(editIdLabel);
		editIdLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editNameLabelPanel = new JPanel();
		editNameLabelPanel.setLayout(new BoxLayout(editNameLabelPanel,
				BoxLayout.X_AXIS));
		editEmployeeLeft.add(editNameLabelPanel);

		JLabel editNameLabel = new JLabel("Name");
		editNameLabelPanel.add(editNameLabel);
		editNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editAddressLabelPanel = new JPanel();
		editAddressLabelPanel.setLayout(new BoxLayout(editAddressLabelPanel,
				BoxLayout.X_AXIS));
		editEmployeeLeft.add(editAddressLabelPanel);

		JLabel editAddressLabel = new JLabel("Address");
		editAddressLabelPanel.add(editAddressLabel);
		editAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editPhoneLabelPanel = new JPanel();
		editPhoneLabelPanel.setLayout(new BoxLayout(editPhoneLabelPanel,
				BoxLayout.X_AXIS));
		editEmployeeLeft.add(editPhoneLabelPanel);

		JLabel editPhoneLabel = new JLabel("Phone");
		editPhoneLabelPanel.add(editPhoneLabel);
		editPhoneLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel editPasswordLabelPanel = new JPanel();
		editEmployeeLeft.add(editPasswordLabelPanel);
		editPasswordLabelPanel.setLayout(new BoxLayout(editPasswordLabelPanel,
				BoxLayout.X_AXIS));

		JLabel editPasswordLabel = new JLabel("Password");
		editPasswordLabel.setHorizontalAlignment(SwingConstants.CENTER);
		editPasswordLabelPanel.add(editPasswordLabel);

		JPanel editPositionLabelPanel = new JPanel();
		editEmployeeLeft.add(editPositionLabelPanel);
		editPositionLabelPanel.setLayout(new BoxLayout(editPositionLabelPanel,
				BoxLayout.X_AXIS));

		JLabel editPositionLabel = new JLabel("Position");
		editPositionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		editPositionLabelPanel.add(editPositionLabel);

		JPanel editEmployeeRight = new JPanel();
		editEmployeeRight.setLayout(new BoxLayout(editEmployeeRight,
				BoxLayout.Y_AXIS));
		editEmployeeInfo.add(editEmployeeRight);

		JPanel editIdFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editEmployeeRight.add(editIdFieldPanel);

		editIdField = new JTextField();
		editIdField.setEditable(false);
		editIdFieldPanel.add(editIdField);
		editIdField.setHorizontalAlignment(SwingConstants.CENTER);
		editIdField.setColumns(5);

		JPanel editNameFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editEmployeeRight.add(editNameFieldPanel);

		editNameField = new JTextField();
		editNameFieldPanel.add(editNameField);
		editNameField.setHorizontalAlignment(SwingConstants.CENTER);
		editNameField.setColumns(20);

		JPanel editAddressFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		editEmployeeRight.add(editAddressFieldPanel);

		editAddressField = new JTextField();
		editAddressFieldPanel.add(editAddressField);
		editAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		editAddressField.setColumns(25);

		JPanel editPhoneFieldPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		editEmployeeRight.add(editPhoneFieldPanel);

		editPhoneField = new JTextField();
		editPhoneFieldPanel.add(editPhoneField);
		editPhoneField.setHorizontalAlignment(SwingConstants.CENTER);
		editPhoneField.setColumns(15);

		JPanel editPasswordFieldPanel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) editPasswordFieldPanel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		editEmployeeRight.add(editPasswordFieldPanel);

		editPasswordField = new JPasswordField();
		editPasswordField.setColumns(15);
		editPasswordFieldPanel.add(editPasswordField);

		JPanel editPositionFieldPanel = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) editPositionFieldPanel
				.getLayout();
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		editEmployeeRight.add(editPositionFieldPanel);

		String[] positionStrings = { "Admin", "Manager", "Salesman", "Other" };

		editPositionCombo = new JComboBox<String>(positionStrings);
		editPositionCombo.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int state = e.getStateChange();
				if (state == ItemEvent.SELECTED) {
					String positionName = (String) e.getItem();
					if (positionName.equals("Other")) {
						editPositionField.setVisible(true);
					} else if (editPositionField.isVisible()) {
						editPositionField.setVisible(false);
					}
				}
			}
		});
		editPositionFieldPanel.add(editPositionCombo);

		editPositionField = new JTextField();
		editPositionField.setHorizontalAlignment(SwingConstants.CENTER);
		editPositionField.setColumns(15);
		editPositionFieldPanel.add(editPositionField);

		JPanel editButtonsPanel = new JPanel();
		editEmployee.add(editButtonsPanel, BorderLayout.CENTER);
		editButtonsPanel.setLayout(new BorderLayout(0, 0));

		JPanel editCenterButtonsPanel = new JPanel();
		editButtonsPanel.add(editCenterButtonsPanel, BorderLayout.CENTER);

		JButton editBtnEdit = new JButton("Edit");
		editBtnEdit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int reply = JOptionPane.showConfirmDialog(null,
						"Are you sure you want to edit this employee ?",
						"Edit Employee", JOptionPane.YES_NO_OPTION,
						JOptionPane.WARNING_MESSAGE);
				if (reply == JOptionPane.YES_OPTION) {
					boolean success = true;
					try {
						personCtr.updateEmployee(
								Integer.parseInt(editIdField.getText()), 1,
								editNameField.getText());
						personCtr.updateEmployee(
								Integer.parseInt(editIdField.getText()), 2,
								editAddressField.getText());
						personCtr.updateEmployee(
								Integer.parseInt(editIdField.getText()), 3,
								editPhoneField.getText());
						personCtr.updateEmployee(
								Integer.parseInt(editIdField.getText()), 4,
								String.valueOf(editPasswordField.getPassword()));
						String position = "";
						if(editPositionCombo.getSelectedIndex() == 0) position = "admin";
						else if(editPositionCombo.getSelectedIndex() == 1) position = "manager";
						else if(editPositionCombo.getSelectedIndex() == 2) position = "salesman";
						else position = editPositionField.getText();
						personCtr.updateEmployee(
								Integer.parseInt(editIdField.getText()), 5,
								position);
					} catch (NumberFormatException e1) {
						success = false;
						JOptionPane.showMessageDialog(null,
								"Field value invalid", "Error",
								JOptionPane.ERROR_MESSAGE);
					} catch (RealException e1) {
						success = false;
						JOptionPane.showMessageDialog(null, e1.getError(),
								"Error", JOptionPane.ERROR_MESSAGE);
					}
					if (success) {
						JOptionPane.showMessageDialog(null,
								"The employee was edited", "Edit Employee",
								JOptionPane.INFORMATION_MESSAGE);
						showEmployee(Integer.parseInt(showIdField.getText()));
					}
				}
			}
		});
		editCenterButtonsPanel.add(editBtnEdit);

		JButton editBtnCancel = new JButton("Cancel");
		editBtnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showEmployee((int) Integer.parseInt(editIdField.getText()));
			}
		});
		editCenterButtonsPanel.add(editBtnCancel);

		createEmployee = new JPanel();
		employeesContainer.add(createEmployee, "createEmployee");
		createEmployee.setLayout(new BorderLayout(0, 0));

		JPanel createEmployeeInfoContainer = new JPanel();
		createEmployee.add(createEmployeeInfoContainer, BorderLayout.NORTH);
		createEmployeeInfoContainer.setLayout(new FlowLayout(FlowLayout.CENTER,
				5, 5));
		createEmployeeInfoContainer.setBorder(BorderFactory
				.createTitledBorder("Create employee"));

		JPanel createEmployeeInfo = new JPanel();
		createEmployeeInfo.setLayout(new BoxLayout(createEmployeeInfo,
				BoxLayout.X_AXIS));
		createEmployeeInfoContainer.add(createEmployeeInfo);

		JPanel createEmployeeLeft = new JPanel();
		createEmployeeInfo.add(createEmployeeLeft);
		createEmployeeLeft.setLayout(new GridLayout(0, 1, 0, 0));

		JPanel createNameLabelPanel = new JPanel();
		createNameLabelPanel.setLayout(new BoxLayout(createNameLabelPanel,
				BoxLayout.X_AXIS));
		createEmployeeLeft.add(createNameLabelPanel);

		JLabel createNameLabel = new JLabel("Name");
		createNameLabelPanel.add(createNameLabel);
		createNameLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createAddressLabelPanel = new JPanel();
		createAddressLabelPanel.setLayout(new BoxLayout(
				createAddressLabelPanel, BoxLayout.X_AXIS));
		createEmployeeLeft.add(createAddressLabelPanel);

		JLabel createAddressLabel = new JLabel("Address");
		createAddressLabelPanel.add(createAddressLabel);
		createAddressLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createPhoneLabelPanel = new JPanel();
		createPhoneLabelPanel.setLayout(new BoxLayout(createPhoneLabelPanel,
				BoxLayout.X_AXIS));
		createEmployeeLeft.add(createPhoneLabelPanel);

		JLabel createPhoneLabel = new JLabel("Phone");
		createPhoneLabelPanel.add(createPhoneLabel);
		createPhoneLabel.setHorizontalAlignment(SwingConstants.CENTER);

		JPanel createPasswordLabelPanel = new JPanel();
		createEmployeeLeft.add(createPasswordLabelPanel);
		createPasswordLabelPanel.setLayout(new BoxLayout(
				createPasswordLabelPanel, BoxLayout.X_AXIS));

		JLabel createPasswordLabel = new JLabel("Password");
		createPasswordLabel.setHorizontalAlignment(SwingConstants.CENTER);
		createPasswordLabelPanel.add(createPasswordLabel);

		JPanel createPositionLabelPanel = new JPanel();
		createEmployeeLeft.add(createPositionLabelPanel);

		JLabel createPositionLabel = new JLabel("Position");
		createPositionLabel.setHorizontalAlignment(SwingConstants.CENTER);
		createPositionLabelPanel.add(createPositionLabel);

		JPanel createEmployeeRight = new JPanel();
		createEmployeeRight.setLayout(new BoxLayout(createEmployeeRight,
				BoxLayout.Y_AXIS));
		createEmployeeInfo.add(createEmployeeRight);

		JPanel createNameFieldPanel = new JPanel(
				new FlowLayout(FlowLayout.LEFT));
		createEmployeeRight.add(createNameFieldPanel);

		createNameField = new JTextField();
		createNameFieldPanel.add(createNameField);
		createNameField.setHorizontalAlignment(SwingConstants.CENTER);
		createNameField.setColumns(20);

		JPanel createAddressFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		createEmployeeRight.add(createAddressFieldPanel);

		createAddressField = new JTextField();
		createAddressFieldPanel.add(createAddressField);
		createAddressField.setHorizontalAlignment(SwingConstants.CENTER);
		createAddressField.setColumns(25);

		JPanel createPhoneFieldPanel = new JPanel(new FlowLayout(
				FlowLayout.LEFT));
		createEmployeeRight.add(createPhoneFieldPanel);

		createPhoneField = new JTextField();
		createPhoneFieldPanel.add(createPhoneField);
		createPhoneField.setHorizontalAlignment(SwingConstants.CENTER);
		createPhoneField.setColumns(15);

		JPanel createPasswordFieldPanel = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) createPasswordFieldPanel
				.getLayout();
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		createEmployeeRight.add(createPasswordFieldPanel);

		createPasswordField = new JPasswordField();
		createPasswordField.setHorizontalAlignment(SwingConstants.CENTER);
		createPasswordField.setColumns(15);
		createPasswordFieldPanel.add(createPasswordField);

		JPanel createPositionFieldPanel = new JPanel();
		FlowLayout flowLayout_4 = (FlowLayout) createPositionFieldPanel
				.getLayout();
		flowLayout_4.setAlignment(FlowLayout.LEFT);
		createEmployeeRight.add(createPositionFieldPanel);
		
		createPositionCombo = new JComboBox<String>(positionStrings);
		createPositionFieldPanel.add(createPositionCombo);
		
		createPositionCombo.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int state = e.getStateChange();
				if (state == ItemEvent.SELECTED) {
					String positionName = (String) e.getItem();
					if (positionName.equals("Other")) {
						createPositionField.setVisible(true);
					} else if (createPositionField.isVisible()) {
						createPositionField.setVisible(false);
					}
				}
			}
		});

		createPositionField = new JTextField();
		createPositionField.setHorizontalAlignment(SwingConstants.CENTER);
		createPositionField.setColumns(15);
		createPositionFieldPanel.add(createPositionField);

		JPanel createBottomMenu = new JPanel();
		createEmployee.add(createBottomMenu, BorderLayout.CENTER);
		createBottomMenu.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		JButton createBtnCreate = new JButton("Create");
		createBottomMenu.add(createBtnCreate);

		JButton createBtnCancel = new JButton("Cancel");
		createBtnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAllEmployees();
			}
		});
		createBottomMenu.add(createBtnCancel);

		showAllEmployees = new JPanel();
		employeesContainer.add(showAllEmployees, "showAllEmployees");
		showAllEmployees.setLayout(new BorderLayout(0, 0));

		JPanel showAllEmployeesPanel = new JPanel();
		showAllEmployeesPanel.setBorder(BorderFactory
				.createTitledBorder("All Employees"));
		showAllEmployees.add(showAllEmployeesPanel, BorderLayout.CENTER);

		table = new JTable(tableModel);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_NEXT_COLUMN);

		table.setAutoCreateRowSorter(true);

		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int row = table.rowAtPoint(e.getPoint());
				if (e.getClickCount() == 2) {
					showEmployee(Integer.valueOf((Integer) tableModel
							.getValueAt(row, 0)));
				}
			}
		});
		showAllEmployeesPanel.setLayout(new BoxLayout(showAllEmployeesPanel,
				BoxLayout.X_AXIS));

		JScrollPane scrollPane = new JScrollPane();
		showAllEmployeesPanel.add(scrollPane);

		scrollPane.setViewportView(table);
		createBtnCreate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				boolean success = true;
				try {
					String position = "";
					if(createPositionCombo.getSelectedIndex() == 0) position = "admin";
					else if(createPositionCombo.getSelectedIndex() == 1) position = "manager";
					else if(createPositionCombo.getSelectedIndex() == 2) position = "salesman";
					else position = createPositionField.getText();
					personCtr.createEmployee(createNameField.getText(),
							createAddressField.getText(),
							Long.parseLong(createPhoneField.getText()),
							String.valueOf(createPasswordField.getPassword()),
							position);
				} catch (NumberFormatException e1) {
					success = false;
					JOptionPane.showMessageDialog(null, "Field value invalid",
							"Error", JOptionPane.ERROR_MESSAGE);
				}
				if (success) {
					JOptionPane.showMessageDialog(null,
							"The employee was created", "Create Employee",
							JOptionPane.INFORMATION_MESSAGE);
					showAllEmployees();
				}
			}
		});
		showAllEmployees();
	}

	protected void showEmployee(int id) {
		boolean success = true;
		Employee employee = null;
		try {
			employee = employeeRecords.getEmployee(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			showIdField.setText(Integer.toString(employee.getId()));
			showNameField.setText(employee.getName());
			showAddressField.setText(employee.getAddress());
			showPhoneField.setText(Long.toString(employee.getPhone()));
			showPasswordField.setText(employee.getPassword());
			showPositionField.setText(employee.getPosition());
			CardLayout cl = (CardLayout) (employeesContainer.getLayout());
			cl.show(employeesContainer, "showEmployee");
		}
	}

	protected void editEmployee(int id) {
		Employee employee = null;
		boolean success = true;
		try {
			employee = employeeRecords.getEmployee(id);
		} catch (RealException e) {
			success = false;
			JOptionPane.showMessageDialog(null, e.getError(), "Error",
					JOptionPane.ERROR_MESSAGE);
		}
		if (success) {
			editIdField.setText(Integer.toString(employee.getId()));
			editNameField.setText(employee.getName());
			editAddressField.setText(employee.getAddress());
			editPhoneField.setText(Long.toString(employee.getPhone()));
			editPasswordField.setText(employee.getPassword());
			if(employee.getPosition().equals("admin"))
			{
				editPositionCombo.setSelectedIndex(1);
				editPositionCombo.setSelectedIndex(0);
			}
			else if(employee.getPosition().equals("manager"))
			{
				editPositionCombo.setSelectedIndex(0);
				editPositionCombo.setSelectedIndex(1);
			}
			else if(employee.getPosition().equals("salesman"))
			{
				editPositionCombo.setSelectedIndex(0);
				editPositionCombo.setSelectedIndex(2);
			}
			else
			{
				editPositionCombo.setSelectedIndex(0);
				editPositionCombo.setSelectedIndex(3);
				editPositionField.setVisible(true);
				editPositionField.setText(employee.getPosition());
			}
			CardLayout cl = (CardLayout) (employeesContainer.getLayout());
			cl.show(employeesContainer, "editEmployee");
		}
	}

	protected void showAllEmployees() {
		ArrayList<Employee> employees = employeeRecords.getEmployees();
		tableModel.setRowCount(0);
		for (Employee employee : employees) {
			tableModel.addRow(new Object[] { employee.getId(),
					employee.getName(), employee.getAddress() });
		}
		CardLayout cl = (CardLayout) (employeesContainer.getLayout());
		cl.show(employeesContainer, "showAllEmployees");
	}

}
